package lk.controller;

import lk.exception.RestfulRecord;
import lk.pojo.Book;
import lk.pojo.BookCollection;
import lk.pojo.BookComment;
import lk.pojo.BookPraise;
import lk.service.BookService;
import lk.util.SessionUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName:BookController
 * Description:
 */
@RestController
@RequestMapping("/book")
public class BookController {

    @Resource
    private BookService bookService;

    @RequestMapping(value = "/addBook", method = RequestMethod.POST)
    public int addBook(@RequestBody Book book) {
        book.setCreateBy(SessionUtil.getUserId());
        return bookService.addBook(book);
    }

    @RequestMapping(value = "/examineBook", method = RequestMethod.GET)
    public RestfulRecord examineBook(Long bookId) {
        if ("0".equals(SessionUtil.getUserInfo().getIsDefault())) {
            return new RestfulRecord(500, "不具备审核权限");
        }
        return new RestfulRecord(200, "审核成功", bookService.examineBook(bookId));
    }

    @RequestMapping(value = "/removeBook", method = RequestMethod.GET)
    public RestfulRecord removeBook(Long bookId) {
        int result = bookService.removeBook(bookId);
        if (result > 0) {
            return new RestfulRecord(200, "删除成功", result);
        }
        return new RestfulRecord(500, "删除失败", result);
    }

    @RequestMapping(value = "/findBook", method = RequestMethod.GET)
    public List<Book> findList(Book book) {
        if ("0".equals(SessionUtil.getUserInfo().getIsDefault())) {
            book.setBookStatus("1");
        }
        //判断是否收藏使用
        book.setCreateBy(SessionUtil.getUserId());
        return bookService.findList(book);
    }

    @RequestMapping(value = "/findMyBook", method = RequestMethod.GET)
    public List<Book> findMyBook(Book book) {
        //判断是否是自己上传 和 是否收藏使用
        book.setCreateBy(SessionUtil.getUserId());
        return bookService.findMyBook(book);
    }

    @RequestMapping(value = "/bookcollection", method = RequestMethod.GET)
    public RestfulRecord bookcollection(Long bookId) {
        BookCollection bookCollection = new BookCollection();
        bookCollection.setBookId(bookId);
        bookCollection.setUserId(SessionUtil.getUserId());
        int result = bookService.bookcollection(bookCollection);
        if (result > 0) {
            return new RestfulRecord(200, "收藏成功", result);
        }
        return new RestfulRecord(500, "收藏失败", result);
    }

    @RequestMapping(value = "findBookCollection", method = RequestMethod.GET)
    public List<BookCollection> findBookCollection(String fileName) {
        BookCollection bookCollection = new BookCollection();
        bookCollection.setFileName(fileName);
        bookCollection.setUserId(SessionUtil.getUserId());
        return bookService.findBookCollection(bookCollection);
    }

    @RequestMapping(value = "removeBookcollection", method = RequestMethod.GET)
    public Object removeBookcollection(Long bookId) {
        BookCollection bookCollection = new BookCollection();
        bookCollection.setBookId(bookId);
        bookCollection.setUserId(SessionUtil.getUserId());
        int result = bookService.removeBookcollection(bookCollection);
        if (result > 0) {
            return new RestfulRecord(200, "取消成功", result);
        }
        return new RestfulRecord(500, "取消失败", result);
    }

    @RequestMapping(value = "/addBookComment", method = RequestMethod.POST)
    public RestfulRecord addBookComment(@RequestBody BookComment bookComment) {
        bookComment.setUserId(SessionUtil.getUserId());
        int result = bookService.addBookComment(bookComment);
        if (result > 0) {
            return new RestfulRecord(200, "评论成功", result);
        }
        return new RestfulRecord(500, "评论失败", result);
    }

    @RequestMapping(value = "findBookComment", method = RequestMethod.GET)
    public List<BookComment> findBookComment(Long bookId) {
        return bookService.findBookComment(bookId);
    }

    @RequestMapping(value = "removeBookComment", method = RequestMethod.GET)
    public Object removeBookComment(Long id) {
        BookComment bookComment = new BookComment();
        bookComment.setId(id);
        bookComment.setUserId(SessionUtil.getUserId());
        int result = bookService.removeBookComment(bookComment);
        if (result > 0) {
            return new RestfulRecord(200, "删除成功", result);
        }
        return new RestfulRecord(500, "删除失败", result);
    }

    @RequestMapping(value = "addBookPraise", method = RequestMethod.GET)
    public Object addBookPraise(Long bookId) {
        BookPraise bookPraise = new BookPraise();
        bookPraise.setBookId(bookId);
        bookPraise.setUserId(SessionUtil.getUserId());
        int result = bookService.addBookPraise(bookPraise);
        if (result>0) {
            return new RestfulRecord(200, "点赞成功", result);
        }
        return new RestfulRecord(500, "重复点赞", result);
    }

}
